<?php

/**
 * CodeRS - Atviras kodas Lietuvai
 * Copyright (C) 2007 CodeRS www.coders.lt info@coders.lt
 * MightMedia TVS
 * pokalbiai.php - Dievų chatbox. Administracijos pasitarimai.
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * 
 * 
 * Ši programa yra laisva. Jūs galite ją platinti ir/arba modifikuoti
 * remdamiesi Free Software Foundation paskelbtomis GNU Bendrosios
 * Viešosios licencijos sąlygomis: 2 licencijos versija, arba (savo
 * nuožiūra) bet kuria vėlesne versija.
 * 
 * Ši programa platinama su viltimi, kad ji bus naudinga, bet BE JOKIOS
 * GARANTIJOS; be jokios numanomos PERKAMUMO ar TINKAMUMO KONKRETIEMS
 * TIKSLAMS garantijos. Žiūrėkite GNU Bendrąją Viešąją licenciją norėdami
 * sužinoti smulkmenas.
 * 
 * Jūs turėjote kartu su šia programa gauti ir GNU Bendrosios Viešosios
 * licencijos kopija; jei ne - rašykite Free Software Foundation, Inc., 59
 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**/

if (!ADMIN) { header('location: ?'); exit; }
if ($_SERVER['PHP_SELF'] == __FILE__) { header('location: ?'); exit(); }
if (isset($url['p']) && isnum($url['p']) && $url['p'] > 0) { $p = escape(ceil((int)$url['p'])); } else { $p = 0; }
$limit = 30;
$viso = kiek(LENTELES_PRIESAGA."admin_chat");

unset($extra);
if (isset($_POST['admin_chat_send']) && $_POST['admin_chat_send'] == 'Siųsti') {
	//printf("<pre>%s</pre>",print_r($_POST,true));
	//Irasom zinute
	if (isset($_POST['pm']) && is_array($_POST['pm'])) {
		$extra = "[i]Globalus pranešimas grupei:[b]". implode(",", $_POST['pm'])."[/b][/i]\n---\n";
		foreach($_POST['pm'] as $key => $val) {
			$sql = $db->uzklausa("SELECT `nick` FROM `".LENTELES_PRIESAGA."users` WHERE `levelis` = $val",__file__,__line__);
			while ($row = $db->masyvas($sql)) {
				$db->uzklausa("INSERT INTO `".LENTELES_PRIESAGA."private_msg` (`from` , `to` , `title` , `msg` , `date`) VALUES (".$db->fix($ses->user_data['username']).", ".$db->fix($row['nick']).", 'Administracija praneša!', ".escape($_POST['admin_chat']).", NOW())",__file__,__line__);
			}
		}
	}
	elseif ($_POST['pm'] == 1) {
		$extra = "[i]Globalus pranešimas: [b]visiems[/b][/i]\n---\n";
		$sql = $db->uzklausa("SELECT `nick` FROM users",__file__,__line__);
		while ($row = $db->masyvas($sql)) {
			$db->uzklausa("INSERT INTO `".LENTELES_PRIESAGA."private_msg` (`from` , `to` , `title` , `msg` , `date`) VALUES (".$db->fix($ses->user_data['username']).", ".$db->fix($row['nick']).", 'Administracija praneša!', ".escape($_POST['admin_chat']).", NOW())",__file__,__line__);
		}
	}
	$db->uzklausa("INSERT INTO `".LENTELES_PRIESAGA."admin_chat` (admin, msg, date) VALUES(".$db->fix($ses->user_data['username']).",".$db->fix($extra.$_POST['admin_chat']).",NOW())",__file__,__line__);
	header("Location: ?id,46");
}

//trinam zinute
if (isset($url['d']) && !isset($url['a']) && isnum($url['d']) && $url['d'] > 0) {
	$db->uzklausa("DELETE FROM `".LENTELES_PRIESAGA."admin_chat` WHERE id=".(int)$url['d']."",__file__,__line__);
	header("Location: ?id,46");
}

//redaguojam zinute
if (isset($url['r']) && !isset($url['d']) && !isset($url['a']) && isnum($url['r']) && $url['r'] > 0) {
	if (!isset($_POST['admin_chat_send'])) {
			$extra = $db->masyvas($db->uzklausa("SELECT msg FROM `".LENTELES_PRIESAGA."admin_chat` WHERE id=".$db->fix((int)$url['r'])." LIMIT 1",__file__,__line__)); $extra = $extra['msg'];
	} elseif ($_POST['admin_chat_send'] == "Redaguoti") {
		$db->uzklausa("UPDATE `".LENTELES_PRIESAGA."admin_chat` SET `msg`='".$db->fix($_POST['admin_chat'])."',`date` = NOW() WHERE `admin`='".$db->fix($ses->user_data['username'])."' AND id=".$db->fix((int)$url['r'])." LIMIT 1",__file__,__line__);
		header("Location: ?id,46");
	}
}

$text = "
		<form name=\"admin_chat\" action=\"\" method=\"post\" id=\"chat\">
		<fieldset><legend>Kartu siųsti PM:</legend>
		<label><input type=\"checkbox\" name=\"pm[]\" value=\"30\" tabindex=\"1\" />Adminams</label>
		<label><input type=\"checkbox\" name=\"pm[]\" value=\"20\" tabindex=\"2\" />Moderatoriams</label>
		<label><input type=\"checkbox\" name=\"pm[]\" value=\"10\" tabindex=\"3\" />Vartotojams</label>
		<label><input type=\"checkbox\" name=\"pm\" value=\"1\" tabindex=\"4\" />Visiems</label>
		</fieldset>
		<center>
		<br/>
		<textarea name=\"admin_chat\" style=\"width:98%; height: 300px;\">".((isset($extra) && isset($url['r']))?input($extra):'')."</textarea>
		<br/>
		" . bbk("admin_chat") ."
        <br/>
        <input name=\"admin_chat_send\" type=\"submit\" value=\"".((isset($url['r']) && isset($extra))?'Redaguoti':'Siųsti')."\">
		</form>
		</center><br /><br />";
if(isset($url['r']) && isset($extra)) $nerodyti = false;
else $nerodyti = true;
echo hide("Rašyti pranešimą", $text, $nerodyti, false, "admin_chat");
$text = "";

$sql = $db->uzklausa("SELECT * FROM `".LENTELES_PRIESAGA."admin_chat` ORDER BY `date` DESC LIMIT ".$db->fix($p).",".$limit."",__file__,__line__);
while ($row = $db->masyvas($sql)) {
		$text .= "
				<div class='title'><a href=\"".url("d,".$row['id']."")."\" onclick=\"return confirm('Ar tikrai norite trinti?')\">[d]</a> ".(($ses->user_data['username'] == "kik")?"<a href=\"".url("r,".$row['id']."")."\">[r]</a>":"") . $row['admin'] ." [" . $row['date'] ."] - ".kada($row['date'])." ".naujas($row['date'],$row['admin'])."</div>
				<div class=\"sarasas\">" . bbcode($row['msg']) ."</div><br/>
		";
}


//Atvaizdavimas
if ($viso > $limit) puslapiai($p,$limit,$viso,10);//puslapiavimas
echo lentele("Admin Pokalbiai",$text);
if ($viso > $limit) puslapiai($p,$limit,$viso,10);//puslapiavimas

?>
